package com.guoliang.eduService.controller;

import com.alibaba.excel.converters.string.StringErrorConverter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guoliang.commonUtils.CommonResult;
import com.guoliang.commonUtils.entity.CourseFrontVo;
import com.guoliang.eduService.entity.EduCourse;
import com.guoliang.eduService.entity.vo.CourseInfoVo;
import com.guoliang.eduService.entity.vo.CoursePublishVo;
import com.guoliang.eduService.entity.vo.CourseQuery;
import com.guoliang.eduService.service.EduCourseService;
import com.sun.org.apache.xml.internal.dtm.ref.sax2dtm.SAX2RTFDTM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.AfterDomainEventPublication;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author leo
 * @since 2023-01-18
 */
@RestController
@RequestMapping("/eduService/course")
@Api(description = "课程信息")
public class EduCourseController {
    @Autowired
    EduCourseService eduCourseService;
    @ApiOperation("课程信息添加")
    @PostMapping("/addCourseInfo")
    public CommonResult addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
       String id=eduCourseService.saveCourseInfo(courseInfoVo);
        return CommonResult.ok().data("id",id);
    }
    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("getCourseInfo/{id}")
    public CommonResult getCourseById(@PathVariable("id")String id){
        CourseInfoVo courseInfoVo = eduCourseService.getByCourseInfoVoById(id);
        return CommonResult.ok().data("courseInfo",courseInfoVo);
    }
    @ApiOperation("修改课程信息")
    @PutMapping("updateCourseInfo")
    public CommonResult updateCourseInfo(@RequestBody CourseInfoVo courseInfo){
        eduCourseService.updateCourseInfo(courseInfo);
        return CommonResult.ok();
    }
    @ApiOperation("根据课程id获取课程确认信息")
    @GetMapping("getCoursePublishInfo/{id}")
    public CommonResult getCoursePublishById(@PathVariable("id")String id){
        CoursePublishVo coursePublishVo=eduCourseService.getCoursePublish(id);
        return CommonResult.ok().data("coursePublish",coursePublishVo);
    }
    @ApiOperation("课程最终发布")
    @PutMapping("publish/{id}")
    public CommonResult publish(@PathVariable("id")String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return CommonResult.ok();
    }
    @ApiOperation("条件查询加分页")
    @PostMapping("pageCourseCondition/{page}/{limit}")
    public CommonResult pageCourseCondition(@RequestBody CourseQuery courseQuery,
                                            @PathVariable("page")Integer page,@PathVariable("limit")Integer limit){
        Page<EduCourse> coursePage=eduCourseService.pageCourseCondition(courseQuery,page,limit);
        return CommonResult.ok().data("total",coursePage.getTotal()).data("rows",coursePage.getRecords());
    }
    @ApiOperation("删除课程")
    @DeleteMapping("{id}")
    public CommonResult deleteCourse(@PathVariable("id")String id){
        eduCourseService.removeCourse(id);
        return CommonResult.ok();
    }
    @ApiOperation("根据课程id获取课程详情 订单用")
    @GetMapping("orderGetCourse/{id}")
    public CourseFrontVo orderGetCourse(@PathVariable String id){
        com.guoliang.eduService.entity.vo.CourseFrontVo courseInfo = eduCourseService.getFrontCourseInfo(id);
        CourseFrontVo courseFrontVo = new CourseFrontVo();
        BeanUtils.copyProperties(courseInfo,courseFrontVo);
        return courseFrontVo;
    }
    @ApiOperation("根据课程id 更新购买后课程buyCount")
    @GetMapping("updateCourseBuyCount/{id}")
    public CommonResult upDateBuyCount(@PathVariable String id){
        eduCourseService.updateBuyCount(id);
        return CommonResult.ok();
    }
    @ApiOperation("根据课程id和当天浏览量 更新课程数据")
    @GetMapping("updateViewCount/{id}/{viewCount}")
    public void updateViewCount(@PathVariable("id")String id, @PathVariable("viewCount")Long viewCount){
        eduCourseService.updateViewCount(id,viewCount);
    }
}
